<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- is-root判断是否是根节点（使得公司部分也能复用组件treeTools） -->
        <treeTools :tree-node="company" :is-root="true" @addDepts="addDepartments" />
        <hr>
        <el-tree :data="departs" :props="defaultProps" default-expand-all>
          <!-- 说明el-tree里面的这个内容 就是插槽内容 => 填坑内容  => 有多少个节点循环多少次 -->
          <!-- scope-scope 是 tree组件传给每个节点的插槽的内容的数据 -->
          <!-- 顺序一定是 执行slot-scope的赋值 才去执行 props的传值 -->
          <treeTools slot-scope="{data}" :tree-node="data" @delDepts="getDepartments" @addDepts="addDepartments" @editDepts="editDepartments" />
        </el-tree>

      </el-card>
      <!-- 弹窗 -->
      <addDepartments ref="addDepts" :show-dialog.sync="showDialog" :node="node" @addDepts="getDepartments" />
    </div>
  </div>
</template>

<script>
import treeTools from '@/views/departments/components/treeTools'
import addDepartments from '@/views/departments/components/addDepartments'
import { getDepartments } from '@/api/departments'
import { transListtoTreeData } from '@/utils/index'
export default {
  components: {
    treeTools,
    addDepartments
  },
  data() {
    return {
      departs: [],
      showDialog: false,
      node: {},
      defaultProps: {
        label: 'name'
      },
      company: { name: '江苏传智播客教育科技股份有限公司', manager: '负责人', id: '' }
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const res = await getDepartments()
      this.departs = transListtoTreeData(res.depts, '')
      console.log(res)
    },
    addDepartments(treeNode) {
      // 弹出弹窗
      this.showDialog = true
      // 将获取的 treeNode 传给弹窗
      this.node = treeNode
    },
    async editDepartments(treeNode) {
      // 调用子组件的方法获取部门的详细数据
      await this.$refs.addDepts.getDepartmentDetail(treeNode.id)
      // 弹出弹出层
      this.showDialog = true
      // 将子组件传递的 treeNode 传给弹窗
      this.node = treeNode
    }
  }
}
</script>

<style>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
